#!/usr/bin/env node

const http = require('http');
const err = console.error,
      url = require('url'),
      qs = require('querystring');

let items = [];

function showPage(res){
  const html = `
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
            <title>todoList</title>
            </head>
            <body>
              <h1>
                To Do List!
              </h1>
              <form method='get' action='/'>
                <input type='text' name='item'/>
                <button type='submit'>Add Item</button>
              </form>
              <ul>
                ${items.map(item => '<li>' + item + '</li>').join('\n')}
              </ul>
            </body>
            </html>
  `
  res.setHeader('Content-Type','text/html');
  res.statusCode = 200;
  res.end(html);
}
function add(req, res) {
  const value = qs.parse(url.parse(req.url).query).item;
  if(typeof value !== 'undefined' && value !== '') 
    items.push(value);
  console.log(items);
  showPage(res);
}

http.createServer((req,res) => {
  if(req.url != '/'){
    res.statusCode = 404;
    res.end('wrong');
    return ;
  }

}).listen(8080);

